<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class admin extends check{
	//构造函数
	public function __construct(){
		parent::__construct();
		$this->group = spClass('m_group')->findAll(null, 'id ASC');
	}
	//版权验证
	public function index(){
		$domain = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80' ? '' : ':'.$_SERVER['SERVER_PORT']));
		$data = array('domain'=>$domain, 'index'=>ltrim($_SERVER['SCRIPT_NAME'],'/'), 'software'=>'vncms', 'version'=>VERSION);
		$result = self::postdata(LICENSE, $data);
		$vncms = json_decode($result,true);
		$status = $vncms['status'];
		$this->vnmess = empty($vncms['message']) ? '' : $vncms['message'];
		$this->menus = spClass('m_gmenu')->check();
		$this->display('admin.tpl.php');
	}
	//修改密码
	public function chpwd(){
		if($this->spArgs('old_pass') && $this->spArgs('new_pass')){
			$oldpwd = trim($this->spArgs('old_pass'));
			$newpwd = trim($this->spArgs('new_pass'));
			if(md5('vonuo' . $oldpwd . 'com') !== $_SESSION['admin']['pswd']){
				$this->error('原始密码错误！',spUrl('admin','chpwd'));
			}
			if(false != spClass('m_admin')->update(array('id'=>$_SESSION['admin']['id']),array('pswd'=>md5('vonuo' . $newpwd . 'com')))){
				$this->success('修改密码成功！',spUrl('admin','chpwd'));
			}else{
				$this->error('修改密码失败！',spUrl('admin','chpwd'));
			}
		}
		$this->display('chpwd.tpl.php');
	}
	//管理员列表
	public function lis(){
		$this->admin = spClass('m_admin')->spLinker()->findAll();
		$this->display('admin_lis.tpl.php');
	}
	//添加管理员
	public function add(){
		$this->display('admin_add.tpl.php');
	}
	public function doadd(){
		if(false != spClass('m_admin')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('admin','add'));
		$rows = $this->spArgs();
		if(isset($rows['pswd2'])) unset($rows['pswd2']);
		$rows['pswd'] = md5('vonuo' . $rows['pswd'] . 'com');
		$rows['time'] = time();
		$rows['status'] = 1;
		if(false != spClass('m_admin')->create($rows)){
			$this->success('添加管理员成功！',spUrl('admin','lis'));
		}else{
			$this->error('添加管理员失败！',spUrl('admin','add'));
		}
	}
	//更新管理员
	public function upd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('admin','lis'));
		$this->admin = spClass('m_admin')->spLinker()->find(array('id'=>$id));
		$this->display('admin_upd.tpl.php');
	}
	public function doupd(){
		$rows = $this->spArgs();
		if(isset($rows['pswd2'])) unset($rows['pswd2']);
		if(isset($rows['code'])) unset($rows['code']);
		if(isset($rows['pswd']) && !empty($rows['pswd'])){
			$rows['pswd'] = md5('vonuo' . $rows['pswd'] . 'com');
		}else{
			unset($rows['pswd']);
		}
		if(false != spClass('m_admin')->update(array('id'=>$rows['id']),$rows)){
			$this->success('更新管理员信息成功！',spUrl('admin','lis'));
		}else{
			$this->error('更新管理员信息失败！',spUrl('admin','lis'));
		}
	}
	//删除管理员
	public function del(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('admin','lis'));
		$admin = spClass('m_admin')->find(array('id'=>$id));
		if($admin['root'] == 1){
			$this->error('禁止操作创始人账号！',spUrl('admin','lis'));
		}elseif($id == $_SESSION['admin']['id']){
			$this->error('禁止删除自己的账号！',spUrl('admin','lis'));
		}
		if(false != spClass('m_admin')->delete(array('id'=>$id))){
			$this->success('删除管理员成功！',spUrl('admin','lis'));
		}else{
			$this->error('删除管理员失败！',spUrl('admin','lis'));
		}
	}
	//切换状态
	public function updst(){
		if(!$idrow = $this->spArgs('id'))	$this->error('请选择要切换状态的编号！',spUrl('admin','lis'));
		for($i=0;$i<count($idrow);$i++){
			$isnormal = spClass('m_admin')->find(array('id'=>$idrow[$i]),null,'status,root');
			if($isnormal['status'] == 0 || $isnormal['root'] == 1){
				$isnormal = 1;
			}else{
				$isnormal = 0;
			}
			spClass('m_admin')->update(array('id'=>$idrow[$i]),array('status'=>$isnormal));
		}
		$this->success('切换状态成功！',spUrl('admin','lis'));
	}
	//检测账号唯一性
	public function checkcode(){
		if(!$code = trim($this->spArgs('code'))) exit;
		if(false != spClass('m_admin')->find(array('code'=>$code))){
			exit('1'); //标识已存在
		}
		exit('0');
	}
	//CURL
	private static function postdata($url, $data){
		if($url == '' || !is_array($data))	return false;
		$ch = @curl_init();
		if(!$ch)	return false;
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
		curl_setopt($ch, CURLOPT_USERAGENT, 'cf5fa7d05e3accc53443c13ddd5ecc78');
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
}
